package main.http;

import java.util.HashMap;

/**
 * Created by hdt3213 on 17/4/17.
 */

public class StaticCache {
	HashMap<String, HttpResponse> map = new HashMap<String, HttpResponse>();
	String baseDir = "./";
	String staticPrefix = "/static/";

	public String getBaseDir() {
		return baseDir;
	}

	public void setBaseDir(String baseDir) {
		this.baseDir = baseDir;
	}

	public String getStaticPrefix() {
		return staticPrefix;
	}

	public void setStaticPrefix(String staticPrefix) {
		this.staticPrefix = staticPrefix;
	}

	public HttpResponse get(String path) {
		String filename = getFilename(path);
		HttpResponse response = map.get(filename);
		if (response == null) {
			response = new HttpResponse();
			response.loadFile(baseDir + filename);
			map.put(filename, response);
		}
		return response;
	}

	private String getFilename(String path) {
		return path.substring(staticPrefix.length());
	}
}
